<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>分类管理</title>
    <div th:replace="admin/_fragments-admin::basicrefrence"></div>
</head>


<!--网页主体-->
<body>

    <!--网页头-->
    <nav th:replace="admin/_fragments-admin::nav"></nav>
    <div th:replace="admin/_fragments-admin::Seconednav"></div>


    <div class="m-tenDistance">
        <div class="ui container">
            <div class="ui segment">

                <table border="1px" class="ui center aligned table">
                    <thead>
                        <tr>
                            <th>编号</th>
                            <th>类型</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody id="typeBody"></tbody>
                    <tfoot>
                        <tr>
                            <th colspan="3">
                                <div class="ui left floated pagination menu">
                                    <a href="/admin/type_add" class="item">新增</a>
                                </div>
                            </th>
                        </tr>
                    </tfoot>
                </table>

            </div>
        </div>
    </div>


    <footer th:replace="admin/_fragments-admin::footer"></footer>


</body>

<script type="text/javascript">

    $(".delBtn").click(function () {
        if(confirm("确认删除该标签吗")){
            $.ajax({
                url:"/admin/deleteType",
                data:{
                    id:this.id
                } ,
                dataType:"text",
                success:function (resp) {
                    alert(resp);
                    location.reload();
                }
            });
        }
    })

    function flushType(TypeList){
        $('#typeBody').empty();
        for (let type of TypeList) {
            $('#typeBody')
                .append("<tr th:height=\"50px\">\n" +
                    "                            <th>"+type.id+"</th>\n" +
                    "                            <th>"+type.name+"</th>\n" +
                    "                            <th>\n" +
                    "                                <button onclick='deleteType("+type.id+")' class=\"ui mini red basic button delBtn\">删除</button>\n" +
                    "                            </th>\n" +
                    "                        </tr>")
        }
    }

    $.ajax({
        url:"/listAllType",
        success:function (resp) {
            if (resp.code === 0) {
                return;
            }
            for(let type of resp.data){
                if (resp.code === 0) {
                    return;
                }
                flushType(resp.data);
            }
        }
    });

    function deleteType(id) {
        $.ajax({
            url:"/admin/deleteType",
            data:{
                id:id
            },
            success:function (resp) {
                if (resp.code === 0) {
                    alert(resp.msg);
                    return;
                }
                flushType(resp.data);
            }
        });
    }


</script>

</html>